import 'package:cached_network_image/cached_network_image.dart';
import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBEngineer/YBBaseRouter/YBApplication.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBColor/YBColor.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBImageStyle/YBImageStyle.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/YBTextStyle/YBTextStyle.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBHelper/yb_chinese_text/yb_chinese_text.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBJsonToModel/YBMineModel/YBMineAuthVipModel.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBJsonToModel/YBMineModel/YBMineUserInfoModel.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBModule/YBMineModule/YBMineMainPage/YBMineScrollInfoEventBus.dart';
import 'package:yb_zsyiji_androidflutter_pro/YBUtils/YBBaseUtils/YBisLoginUtils.dart';



class YBMineSubAppBarPage extends StatelessWidget {

  final YBMineUserInfoModel userInfoModel;

  const YBMineSubAppBarPage({Key key, this.userInfoModel}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Widget authWidget;

    Widget vipWidget;

    Widget imageWidget;

    Widget headerWidget;

    int scrollInfo = 0;

    mineScrollInfoEventBus.on<YBMineScrollInfoEventBus>().listen((event) {
        scrollInfo = event.scrollInfo;
    });

    if (YBisLoginUtils.isLogin()) {
      if (userInfoModel.certStatus != 2) {
        authWidget = Container(
          child: InkWell(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                  child: Text(
                    TextUtil.hideNumber(userInfoModel.customerPhone)??"",
                    style: YBTextStyle.TextStyle18181820Medium,
                  ),
                ),
                Container(
                  height: YBTextStyle.adapterSize4,
                ),
                Container(
                  height: YBTextStyle.adapterSize20,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.all(Radius.circular(YBTextStyle.adapterSize10)),
                    color: YBColor.colorE8EAEB,
                  ),
                  child: Padding(
                    padding:EdgeInsets.only(left: YBTextStyle.adapterSize10,right: YBTextStyle.adapterSize10,top: YBTextStyle.adapterSize2,bottom: YBTextStyle.adapterSize2),
                    child: Row(
                      children: [
                        Container(
                          child: Text(YBChineseText.mineUnAuthTitle,style: YBTextStyle.TextStyle6C717A10Medium,),
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
            onTap: () async {
              YBApplication.router.navigateTo(context, '/mine/myUserInfoPage');
            },
          ),
        );
      }else{
        authWidget = InkWell(
          child: Container(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                  child: Text(
                    TextUtil.hideNumber(userInfoModel.customerPhone)??"",
                    style: YBTextStyle.TextStyle18181820Medium,
                  ),
                ),
                Container(
                  height: YBTextStyle.adapterSize4,
                ),
                Container(
                  height: YBTextStyle.adapterSize20,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.all(Radius.circular(YBTextStyle.adapterSize10)),
                    color: YBColor.colorE8EAEB,
                  ),
                  child: Padding(
                    padding:EdgeInsets.only(left: YBTextStyle.adapterSize10,right: YBTextStyle.adapterSize10,top:YBTextStyle.adapterSize2,bottom:YBTextStyle.adapterSize2),
                    child: Row(
                      children: [
                        Container(
                          child: Image.asset(YBImageStyle.getImagePath('mineNew_renzhenggreenImageView')),
                        ),
                        Container(
                          width: YBTextStyle.adapterSize4,
                        ),
                        Container(
                          child: Text(YBChineseText.mineAuthTitle,style: YBTextStyle.TextStyle6C717A10Medium,),
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
          onTap: () async {
            YBApplication.router.navigateTo(context, '/mine/myUserInfoPage');
          },
        );
      }

      headerWidget = Container(
        child: Row(
          children: [
            Container(
              height: YBTextStyle.adapterSize30,
              width: YBTextStyle.adapterSize30,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.all(
                  Radius.circular(YBTextStyle.adapterSize15),
                ),
                image: DecorationImage(
                  image: CachedNetworkImageProvider(userInfoModel.avatarUrl??""),
                  fit: BoxFit.cover,
                ),
              ),
            ),
            Container(
              width: YBTextStyle.adapterSize10,
            ),
            Container(
              child: Text(
                TextUtil.hideNumber(userInfoModel.customerPhone)??"",
                style: YBTextStyle.TextStyle18181812Medium,
              ),
            ),
          ],
        ),
      );

    }else{
      authWidget = InkWell(
        child: Container(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                child: Text(
                  YBChineseText.mineLogonInTitle??"",
                  style: YBTextStyle.TextStyle18181820Medium,
                ),
              ),
            ],
          ),
        ),
        onTap: () async {
          YBApplication.router.navigateTo(context, '/loginIn');
        },
      );
      headerWidget = Container(
        child: Row(
          children: [
            Container(
              child: Text(YBChineseText.mineMineTitle),
            ),
          ],
        ),
      );
    }

    if (userInfoModel.customerAuthorities != null) {
      for(YBMineAuthVipModel mineAuthModel in userInfoModel.customerAuthorities){
        if (mineAuthModel.authority.contains('BLVIP')) {
          vipWidget = Container(
            alignment: Alignment.bottomRight,
            child: Image.asset(YBImageStyle.getImagePath('mineNew_vipblueImageView')),
          );
          break;
        }else{
          vipWidget = Container();
        }
      }
    }else{
      vipWidget = Container();
    }

    if (userInfoModel.avatarUrl != null){
      imageWidget = Container(
        height: YBTextStyle.adapterSize56,
        width: YBTextStyle.adapterSize56,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(YBTextStyle.adapterSize28)),
        ),
        child: Stack(
          children: [
            Container(
              decoration: BoxDecoration(
                borderRadius: BorderRadius.all(
                  Radius.circular(YBTextStyle.adapterSize28),
                ),
                image: DecorationImage(
                  image: CachedNetworkImageProvider(userInfoModel.avatarUrl??""),
                  fit: BoxFit.cover,
                ),
              ),
            ),
            Container(
              child: vipWidget,
            ),
          ],
        ),
      );
    }else{
      imageWidget = Container(
        height: YBTextStyle.adapterSize56,
        width: YBTextStyle.adapterSize56,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(YBTextStyle.adapterSize28)),
        ),
        child: Stack(
          children: [
            Container(
              child: ClipRRect(
                borderRadius: BorderRadius.circular(YBTextStyle.adapterSize28),
                child:Image.asset(YBImageStyle.getImagePath('common_headerdefault')),
              ),
            ),
            Container(
              child: vipWidget,
            ),
          ],
        ),
      );
    }
    return SliverAppBar(
      expandedHeight: YBTextStyle.adapterSize88,
      pinned: true,
      elevation: 0,
      title: scrollInfo > 0?headerWidget:null,
      flexibleSpace: FlexibleSpaceBar(
        background: Container(
          color: YBColor.colorF7F8FA,
          child: Padding(
            padding: EdgeInsets.only(left: YBTextStyle.adapterSize20,right:YBTextStyle.adapterSize20),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                imageWidget,
                Container(
                  width: YBTextStyle.adapterSize16,
                ),
                authWidget,
                Container(
                  child: Spacer(),
                ),
                Container(
                  child:InkWell(
                    child: Image.asset(YBImageStyle.getImagePath('mine_messagexiaoImageView')),
                    onTap: (){
                      if(YBisLoginUtils.isLogin() == true){
                        YBApplication.router.navigateTo(context, '/mine/mineMessagePage');
                      }else{
                        YBApplication.router.navigateTo(context, '/loginIn');
                      }
                    },
                  ),
                ),
                Container(
                  width: YBTextStyle.adapterSize15,
                ),
                Container(
                  child:InkWell(
                    child: Image.asset(YBImageStyle.getImagePath('mine_set')),
                    onTap: (){
                      if(YBisLoginUtils.isLogin() == true){
                        YBApplication.router.navigateTo(context, '/mine/mineSettingPage');
                      }else{
                        YBApplication.router.navigateTo(context, '/loginIn');
                      }
                    },
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}




